library(tidyverse)
library(haven)
library(gtsummary)
library(skimr)
library(dplyr)
library(flextable)
library(installr)
library(ggplot2)
library(bibliometrix) 
library(formattable)
library(tableHTML)

# https://www.bibliometrix.org/vignettes/Introduction_to_bibliometrix.html


#基于2020-2021年已发表的285篇文献，对现有文献进行文献计量学分析
#scopus 检索式  TITLE-ABS-KEY ( telemedicine OR telehealth AND covid-19 AND innovation ) AND PUBYEAR > 2019 AND PUBYEAR < 2022 AND ( LIMIT-TO ( LANGUAGE , "English" ) ) AND ( LIMIT-TO ( EXACTKEYWORD , "Telemedicine" ) )
file<- c('scopus.bib')
getwd()
M <- convert2df(file = file, dbsource = "scopus", format = "bibtex")
results <- biblioAnalysis(M, sep = ";")
options(width=100)
S <- summary(object = results, k = 10, pause = FALSE)



MainInformation<-as.data.frame(S$MainInformation)
MostProdAuthors<-as.data.frame(S$MostProdAuthors)
MostCitedPapers<-as.data.frame(S$MostCitedPapers)
MostProdCountries<-as.data.frame(S$MostProdCountries)
TCperCountries<-as.data.frame(S$TCperCountries)
MostRelSources<-as.data.frame(S$MostRelSources)
MostRelKeywords<-as.data.frame(S$MostRelKeywords)

write_tableHTML(tableHTML(MainInformation), file = 'table1.1.html')
write_tableHTML(tableHTML(MostProdAuthors), file = 'table1.2.html')
write_tableHTML(tableHTML(MostCitedPapers), file = 'table1.3.html')
write_tableHTML(tableHTML(MostProdCountries), file = 'table1.4.html')
write_tableHTML(tableHTML(TCperCountries), file = 'table1.5.html')
write_tableHTML(tableHTML(MostRelSources), file = 'table1.6.html')
write_tableHTML(tableHTML(MostRelKeywords), file = 'table1.7.html')

# 画图最多发表国家

# biblioshiny()

# plot(x = results, k = 10, pause = FALSE) 

as <-plot(x = results, k = 10, pause = FALSE)
# 需要把结果存起来 用哪个画图画哪个 2023年11月3日10:41:08 
Cairo::CairoTIFF(file="countrypro.tiff", width=8, height=8,units="in",dpi=150)
as[[2]]
dev.off()

# tab2 
NetMatrix <- biblioNetwork(M, analysis = "co-occurrences", network = "keywords", sep = ";")
netstat <- networkStat(NetMatrix)
summary(netstat)
table2 <-data.frame(info=c('Size','Density','Transitivity','Diameter','Degree Centralization','Average path length'),va=c(2263,0.043 ,0.209 ,3,0.93,1.973))
write_tableHTML(tableHTML(table2), file = 'table2.html')


# fig2 
NetMatrix <- biblioNetwork(M, analysis = "co-occurrences", network = "keywords", sep = ";")
# Plot the network
Cairo::CairoTIFF(file="Network of Keyword Co-occurrences.tiff", width=8, height=8,units="in",dpi=150)
net=networkPlot(NetMatrix, normalize="association", weighted=T, n = 20, Title = "Keyword Co-occurrences", type = "kamada", size=T,edgesize = 5,labelsize=0.7)
dev.off()
#fig3 
CS <- conceptualStructure(M,field="ID", method="CA", minDegree=4, clust=5, stemming=FALSE, labelsize=10, documents=10)
Cairo::CairoTIFF(file="Conceptual Structure Map using Multiple Correspondence Analysis.tiff", width=8, height=8,units="in",dpi=150)
plot(CS[[4]])
dev.off()

# data(management, package="bibliometrixData")
# management <- metaTagExtraction(management, Field = "AU_CO")
# 
# NetMatrix <- biblioNetwork(management, analysis = "collaboration", network = "countries") 
# 
# net <- collabByRegionPlot(NetMatrix, edgesize = 4, label.cex = TRUE, labelsize=2.5, 
#                           weighted = TRUE, size=0.5, size.cex=TRUE, community.repulsion = 0, 
#                           verbose=FALSE)
# 
# cbind(names(net))
# plot(net[[4]]$graph)

